package cafe.ejb;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import cafe.ejb.myexceptions.EntityNotFoundException;
import cafe.ejb.myexceptions.NoLinksException;
import cafe.ejb.serviceinterfaces.WaiterServiceInterface;
import cafe.ejb.serviceinterfacesimpls.WaiterServiceInterfaceImpl;
import cafe.jpa.Order;
import cafe.jpa.Waiter;

/**
 * Session Bean implementation class WaiterServiceBean
 */
@Stateless
@Local(WaiterService.class)
public class WaiterServiceBean implements WaiterService {
	
	@PersistenceContext(unitName="cafe.jpa")
	private EntityManager em;
	
	public WaiterServiceBean() {}

	@Override
	public Waiter getByID(long id) throws EntityNotFoundException, SQLException {		
		Query q = em.createQuery("select w from Waiter w where w.id = :id");
		q.setParameter("id", id);
        return (Waiter) q.getSingleResult();
	}

	@Override
	public List<Waiter> getAllWaiters() throws SQLException, EntityNotFoundException {
		Query q = em.createQuery("select w from Waiter w");        
        return (List<Waiter>) q.getResultList();       
	}

	@Override
	public List<Waiter> getAllWaitersByName(int searchType, String... params) throws EntityNotFoundException, SQLException {		
		List<Waiter> waiters = getAllWaiters();
		
		List<Waiter> rez = new ArrayList<Waiter>();		
		
		if(searchType == WaiterService.SEARCH_BY_ALL) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getFirstName().equals(params[0]) && 
						current.getLastName().equals(params[1]) &&
						current.getPatronymicName().equals(params[2])) {
						rez.add(current);	
					}	
			}
		}
		else if(searchType == WaiterService.SEARCH_BY_FIRSTNAME_AND_LASTNAME) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getFirstName().equals(params[0]) && 
						current.getLastName().equals(params[1]) ) {
						rez.add(current);	
					}	
			}
		}
		else if(searchType == WaiterService.SEARCH_BY_FIRSTNAME_AND_PATRONYMICNAME) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getFirstName().equals(params[0]) && 
						current.getPatronymicName().equals(params[1]) ) {
						rez.add(current);	
					}		
			}
		}
		else if(searchType == WaiterService.SEARCH_BY_FIRSTNAME) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getFirstName().equals(params[0]) ) {
						rez.add(current);	
					}		
			}
		}
		else if(searchType == WaiterService.SEARCH_BY_LASTNAME) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getLastName().equals(params[0]) ) {
						rez.add(current);	
					}		
			}
		}		
		else if(searchType == WaiterService.SEARCH_BY_PATRONYMICNAME) {
			for(int i = 0; i < waiters.size(); i++) {
				Waiter current = waiters.get(i);				
					if(current.getPatronymicName().equals(params[0]) ) {
						rez.add(current);	
					}		
			}
		}
		if(rez.size() == 0)
			throw new EntityNotFoundException();
		
		return rez;		
	}

	@Override
	public void addWaiter(Waiter waiter) throws SQLException {
		em.persist(waiter);
	}

	@Override
	public void removeByID(long id) throws EntityNotFoundException, SQLException {		
		Query q = em.createQuery("DELETE FROM Waiter w	WHERE w.id = :id");
		q.setParameter("id", id);
        q.executeUpdate();		 
	}

	@Override
	public Set<Order> getWaitersOrders(long waiterId) throws EntityNotFoundException, SQLException, NoLinksException {
		
		Waiter waiter = getByID(waiterId);
		return waiter.getOrders();	
	}

	@Override
	public void updateWaiter(long id, int field, Object value)	throws EntityNotFoundException, SQLException {
		Waiter waiter = getByID(id);
		Query query;
		
		if(waiter != null) {				
			switch(field) {
			case Waiter.FIRSTNAME_UPDATE:					
				query = em.createQuery("update WAITER w set w.firstName=:firstname where w.id=:id");
				query.setParameter("firstname", (String)value);
				query.setParameter("id", id);
				query.executeUpdate();
				break;
			case Waiter.LASTNAME_UPDATE: 		
				query = em.createQuery("update WAITER w set w.lastName=:lastname where w.id=:id");
				query.setParameter("lastname", (String)value);
				query.setParameter("id", id);
				query.executeUpdate();
				break;							
			case Waiter.PATRONYMICNAME_UPDATE:
				query = em.createQuery("update WAITER w set w.patronymicName=:patronymicname where w.id=:id");
				query.setParameter("patronymicname", (String)value);
				query.setParameter("id", id);
				query.executeUpdate();
				break;							
			case Waiter.BORN_DAY_UPDATE:
				break;							
			case Waiter.BORN_MONTH_UPDATE:
				break;	
			case Waiter.BORN_YEAR_UPDATE:							
				break;
			case Waiter.HIRE_DAY_UPDATE: 							
				break;							
			case Waiter.HIRE_MONTH_UPDATE: 							
				break;							
			case Waiter.HIRE_YEAR_UPDATE:							
				break;
			case Waiter.SALARY_UPDATE: 
				query = em.createQuery("update WAITER w set w.salary=:salary where w.id=:id");
				query.setParameter("salary", (String)value);
				query.setParameter("id", id);
				query.executeUpdate();
				break;						
			default: 
				throw new IllegalArgumentException();
			}
		}
	}
}
